/**
 * 
 */
package src.model.gameObject.ability;

import src.model.gameObject.GameObject;
import src.model.gameObject.eventBrokers.HealthEventBroker;
import src.model.gameObject.eventBrokers.MeleeEventBroker;
import src.model.gameObject.eventInterfaces.HealthPublisher;
import src.model.gameObject.eventInterfaces.MeleeEventListener;
import src.model.gameWorld.LookupTable;

/**
 * <b>Overview</b><br>
 * <p>This allows a GameObject to counterattack anything that melee attacks it. It only counterattacks melee
 * attacks, not ranged attacks.</p>
 * <br><br>
 * <b>Responsibilites</b><br>
 * <p>Give damage to things that melee attack against it.</p>
 * <br><br>
 * <b>Collaborators</b>
 * <br>none<br>
 * <b>Implementor</b> - Tristan Lohman<br>
 * <b>Tester</b> - Brent<br>
 * @author Tristan
 *
 */
public class CounterAttack extends Ability implements MeleeEventListener, HealthPublisher{

	/**
	 * Constructs a new CounterAttack and registers it with the necessary brokers.
	 * @param go The GameObject that owns this.
	 * @param lookup the LookupTable for this.
	 */
	public CounterAttack(GameObject go, LookupTable lookup) {
		super(go, AbilityType.COUNTERATTACK, lookup);
		MeleeEventBroker.register(super.gameObject(), this);
	}

	/**
	 * MeleeEventBroker notifies this of a MeleeAttackEvent, and starts the counter attack process.
	 * @param go The GameObject that attacked.
	 */
	public void notifyMeleeEvent(GameObject go) {
		float strength = super.getAttribute(AbilityAttributes.STRENGTH);
		float damage = (float)Math.random() * (strength/2) + (strength/2);
		publishHealthModifier(go, damage);
	}
	
	public void publishHealthModifier(GameObject go, float modifyAmount) {
		HealthEventBroker.notifyModifyHealth(go, modifyAmount);
	}
	
	public void destroySub(){
		MeleeEventBroker.unregister(super.gameObject());
	}
}
